package com.team20.map_demo.controller;

import com.team20.map_demo.domain.Region;
import com.team20.map_demo.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private RegionService regionService;

    @RequestMapping("/toadd")
    public String addRegionPage(Model model){
        model.addAttribute("region",new Region());
        return "region_add";
    }

    @RequestMapping("/add")
    public String addRegion(Region region){
        int updated = regionService.saveRegion(region);
        return "redirect:/Region/district-list";
    }

    @RequestMapping("/list")
    public String RegionList(){
        return "district-list";
    }

        @GetMapping(value="edit")
    public String editRegion(@RequestParam("id")String id, Model model){
        Region region=regionService.findRegionByPrimaryKey(id);
        return "region_add";
    }

    @RequestMapping("/edit")
    public String EditRegion(@RequestParam("id") String id, Model model){
        Region region = regionService.findRegionByPrimaryKey(id);
        model.addAttribute("region",region);
        return "region_add";
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteRegion(@RequestParam("id") String id){
        int deleted = regionService.deleteRegion(id);
        return deleted > 0;
    }
  @RequestMapping("/exportRegionInfo.action")
    public void exportRegionInfo(HttpServletResponse response ) throws IOException{
        response.setCharacterEncoding("UTF-8");
        List<Region> list =    regionService.queryAllRegionInfoToExcel();
        System.out.println(list);
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("行政区表");

        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("行政区Id");
        titleRow.createCell(1).setCellValue("姓名");
        titleRow.createCell(2).setCellValue("行政区编码");
        //遍历将数据放到excel列中
        for (Region region : list) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(region.getId());
            dataRow.createCell(1).setCellValue(region.getName());
            dataRow.createCell(2).setCellValue(region.getZip());
        }
                  /*   // 设置下载时客户端Excel的名称
             String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=" + filename);  */

        // 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("行政区规划名单".getBytes(),"iso-8859-1") + ".xls");


        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}

